package com.yuan.Service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuan.Mapper.CategoryMapper;
import com.yuan.Mapper.DishMapper;
import com.yuan.Mapper.SetmealMapper;
import com.yuan.Service.CategoryService;
import com.yuan.constant.MessageConstant;
import com.yuan.constant.StatusConstant;
import com.yuan.context.BaseContext;
import com.yuan.dto.CategoryDTO;
import com.yuan.dto.CategoryPageQueryDTO;
import com.yuan.entity.Category;
import com.yuan.entity.Dish;
import com.yuan.entity.Setmeal;
import com.yuan.exception.DeletionNotAllowedException;
import com.yuan.result.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void saveSelf(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        category.setStatus(StatusConstant.ENABLE);
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        save(category);
    }

    @Override
    public PageResult pageSelf(CategoryPageQueryDTO categoryPageQueryDTO) {
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        String name = categoryPageQueryDTO.getName();
        Integer type = categoryPageQueryDTO.getType();


        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();

        if (name != null && name != "") {
            wrapper.eq(Category::getName, name);
        }
        if (type != null && type != 0) {
            wrapper.eq(Category::getType, type);
        }
        Page<Category> p = new Page<>(page, pageSize);

        Page<Category> paged = page(p, wrapper);
        return new PageResult(paged.getTotal(), paged.getRecords());

    }

    @Override
    public void removeByIdSelf(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Dish::getCategoryId, id);
        Long count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId, id);
        Long count1 = setmealMapper.selectCount(wrapper1);

        if (count1 > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        removeById(id);
    }

    @Override
    public void updateSelf(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        updateById(category);

    }

    @Override
    public void startOrStop(Integer status, Long id) {

        Category category = getById(id);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        category.setStatus(status);

        updateById(category);

    }

    @Override
    public List<Category> listSelf(Integer type) {

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType, type);

        List<Category> list = list(wrapper);

        return list;
    }


}
